package com.cdream.basic.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.simple.SimpleDataSource;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toMap;

public class EasyExcelUtilExample {
    private static DataSource ds = new SimpleDataSource("jdbc:mysql://localhost:3306/kernel", "root", "root");
    private static List<List<String>> idList = new ArrayList<>();

    public static void main(String[] args) throws SQLException {
        List<Entity> pimCategory = Db.use(ds).findAll("pimCategory");
        List<TreeNode> treeNode = pimCategory.stream().map(a -> a.toBean(TreeNode.class)).collect(Collectors.toList());
        Map<String, TreeNode> idMap = treeNode.stream().collect(toMap(a -> a.getId(), a -> a));
        Map<String, List<TreeNode>> map = treeNode.stream().collect(Collectors.groupingBy(a -> a.getParentId()));
        List<TreeNode> root = treeNode.stream().filter(a -> a.getLevel() == 1).collect(Collectors.toList());
        for (TreeNode node : root) {
            buildTree(node, map);
        }
        for (TreeNode node : root) {

            dfs(node, 1, CollUtil.newArrayList(node.getId()));

        }
        List<ExcelDemo> realRes = idList.stream().map(a -> {
            ExcelDemo excelDemo = new ExcelDemo();
            TreeNode t1 = idMap.get(a.get(0));
            excelDemo.setCategoryCode1(t1.getCategoryCode());
            excelDemo.setCategoryName1(t1.getCategoryName());
            TreeNode t2 = idMap.get(a.get(1));
            excelDemo.setCategoryCode2(t2.getCategoryCode());
            excelDemo.setCategoryName2(t2.getCategoryName());
            if (a.size() == 2) {
                return excelDemo;
            }
            TreeNode t3 = idMap.get(a.get(2));
            excelDemo.setCategoryCode3(t3.getCategoryCode());
            excelDemo.setCategoryName3(t3.getCategoryName());
            return excelDemo;
        }).collect(Collectors.toList());

        EasyExcel.write("/Users/apple/Desktop/类目.xlsx").sheet("类目列表").doWrite(realRes);

    }

    private static void dfs(TreeNode node, int level, List<String> l) {
        if(CollUtil.isNotEmpty(node.getChildren())){
            for (TreeNode child : node.getChildren()) {
                l.add(child.getId());
                dfs(child,level+1,l);
                l.remove(l.size()-1);
            }
        }else{
            idList.add(new ArrayList<>(l));
        }


    }

    private static void buildTree(TreeNode node, Map<String, List<TreeNode>> map) {
        List<TreeNode> children = map.get(node.getId());
        if (CollUtil.isEmpty(children)) {
            return;
        }
        node.setChildren(children);
        for (TreeNode child : children) {
            buildTree(child, map);
        }
    }


    @Data
    static class ExcelDemo {
        @ExcelProperty(value = "一级类目编码", index = 0)
        private String categoryCode1;
        @ExcelProperty(value = "一级类目名称", index = 1)
        private String categoryName1;
        @ExcelProperty(value = "一级类目编码", index = 2)
        private String categoryCode2;
        @ExcelProperty(value = "一级类目名称", index = 3)
        private String categoryName2;
        @ExcelProperty(value = "一级类目编码", index = 4)
        private String categoryCode3;
        @ExcelProperty(value = "一级类目名称", index = 5)
        private String categoryName3;
    }

    @Data
    static class TreeNode {
        private String id;
        private Integer level;
        private String categoryCode;
        private String categoryName;
        private String parentId;
        private List<TreeNode> children;
    }
}
